package ca.kooki.codesnippet.core;

import java.util.Hashtable;

public class Language {

  //```````````````````````
  // STATIC VARIABLES
  //```````````````````````

  private static final Language singleton = new Language();
  
  //```````````````````````
  // MEMBER VARIABLES
  //```````````````````````

  private Hashtable<String, String> _allEnglish;
  
  //```````````````````````
  // CONSTRUCTOR
  //```````````````````````
  
  public Language() {
    init();
  }
  
  private void init()
  {
    _allEnglish = new Hashtable<String, String>();
    add("success","Success!");
    add("existing_user_name","Username already in use");
    add("empty_user_name","Please provide a username");
    add("no_code_snippet","Please provide code snippet");
    add("no_user_for_tag","Please set your username in the preferences");
    add("repository_configured_properly","Your repository is configured properly");
    add("problem_with_repository_file","Unable to use @1, due to @2");
    add("unable_create_repository_directory","Unable to create repository directory @1");
  }
  

  public static Language getDefault()
  {
    return singleton;
  }
  
  public static void reset()
  {
    singleton.init();
  }
  
  //```````````````````````
  // LOOKUP
  //```````````````````````
  
  public void add(String id, String english)
  {
    _allEnglish.put(id, english);
  }
  
  public String lookup(String id)
  {
    return lookup(id,true);
  }
  
  public String lookup(String id, boolean isStrict)
  {
    String defaultText = isStrict ? "UNKNOWN: " + id : id;
    return _allEnglish.containsKey(id) ? _allEnglish.get(id) : defaultText;
  }

  public String lookup(String id, String[] allReplacements)
  {
    return lookup(id,true,allReplacements);
  }
  
  public String lookup(String id, boolean isStrict, String[] allReplacements)
  {
    String text = lookup(id,isStrict);
    for (int i=1; i<=allReplacements.length; i++)
    {
      text = text.replace("@" + i, allReplacements[i-1]);
    }
    return text;
  }
  
}
